﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ex1.ComplexNumber
{
    class ComplexNumber
    {
        private double realPart;
        private double imaginaryPart;

        public ComplexNumber(double realPart, double imaginaryPart)
        {
            this.realPart = realPart;
            this.imaginaryPart = imaginaryPart;
        }

        public double RealPart
        {
            get
            {
                return realPart;
            }
            set
            {
                realPart = value;
            }
        }
        public double ImaginaryPart
        {
            get
            {
                return imaginaryPart;
            }
            set
            {
                imaginaryPart = value;
            }
        }

        public static ComplexNumber operator +(ComplexNumber numberOne, ComplexNumber numberTwo)
        {
            ComplexNumber result = new ComplexNumber(numberOne.RealPart + numberTwo.RealPart, 
                numberOne.ImaginaryPart + numberTwo.ImaginaryPart);

            return result;
        }

        public static ComplexNumber operator -(ComplexNumber numberOne, ComplexNumber numberTwo)
        {
            ComplexNumber result = new ComplexNumber(numberOne.RealPart - numberTwo.RealPart, 
                numberOne.ImaginaryPart - numberTwo.ImaginaryPart);

            return result;
        }

        public static ComplexNumber operator *(ComplexNumber numberOne, ComplexNumber numberTwo)
        {
            double resultReal = numberOne.RealPart * numberTwo.RealPart - 
                numberOne.ImaginaryPart * numberTwo.ImaginaryPart;

            double resultImaginary = numberOne.ImaginaryPart * numberTwo.RealPart + 
                numberOne.RealPart * numberTwo.ImaginaryPart;

            return new ComplexNumber(resultReal, resultImaginary);
        }

        public static ComplexNumber operator /(ComplexNumber numberOne, ComplexNumber numberTwo)
        {
            double resultReal = 
                (numberOne.RealPart * numberTwo.RealPart + numberOne.ImaginaryPart * numberTwo.ImaginaryPart) /
               (numberTwo.RealPart * numberTwo.RealPart + numberTwo.ImaginaryPart * numberTwo.ImaginaryPart);

            double resultImaginary = 
                (numberOne.ImaginaryPart * numberTwo.RealPart - numberOne.RealPart * numberTwo.ImaginaryPart) /
                (numberTwo.RealPart * numberTwo.RealPart + numberTwo.ImaginaryPart * numberTwo.ImaginaryPart);

            return new ComplexNumber(resultReal, resultImaginary);
        }

        public static explicit operator double(ComplexNumber c1)
        {
            return c1.RealPart;
        }

        public double this[int index]
        {
            get
            {
                if (index >= 0 && index <= 1)
                {
                    if (index == 0)
                    {
                        return this.RealPart;
                    }
                    else
                    {
                        return this.ImaginaryPart;
                    }
                }

                throw new IndexOutOfRangeException("Index must be in bounds [0,1]");
            }
        }

        public override string ToString()
        {
            return string.Format("{0}+{1}i", this.RealPart, this.ImaginaryPart);
        }
    }
}
